﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GroundWater.Data;
using GroundWater.BusinessActions;

using MonitoringTable = GroundWater.Data.MonitoringDataSet.MonitoringDataTable;
using MonitoringRow = GroundWater.Data.MonitoringDataSet.MonitoringRow;

namespace GroundWater.waterlevel
{
    public partial class VoltageAbnormal : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUtil.Validate(this, "电池检查");
            if (!this.IsPostBack)
            {
                txtStartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");


                string difference = System.Configuration.ConfigurationManager.AppSettings["MinimumVoltage"];
                txtDifference.Text = difference;

                

                btnCheck_Click(null, EventArgs.Empty);
            }
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs args)
        {
            GridView gridView = (GridView)sender;

            if (gridView.SortExpression.Length > 0)
            {
                int cellIndex = -1;
                foreach (DataControlField field in gridView.Columns)
                {
                    if (field.SortExpression == gridView.SortExpression)
                    {
                        cellIndex = gridView.Columns.IndexOf(field);
                        break;
                    }
                }

                if (cellIndex > -1)
                {
                    if (args.Row.RowType == DataControlRowType.Header)
                    {
                        //  this is a header row,
                        //  set the sort style
                        args.Row.Cells[cellIndex].CssClass +=
                            (gridView.SortDirection == SortDirection.Ascending
                            ? " sortasc" : " sortdesc");
                    }
                }
            }

        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            #region validate
            
            if (!Husb.Util.CommonUtil.IsNumber(txtDifference.Text))
            {
                return;
            }
            
            if (txtStartDate.Text.Length < 6)
            {
                txtStartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (txtEndDate.Text.Length < 6)
            {
                txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }

            DateTime endDate1;
            DateTime startDate1;


            if (!DateTime.TryParse(txtStartDate.Text, out startDate1) || !DateTime.TryParse(txtEndDate.Text, out endDate1))
            {
                return;
            }
            #endregion

            string startDate = txtStartDate.Text;
            string endDate = txtEndDate.Text;

            if (txtDifference.Text.Length == 0)
            {
                txtDifference.Text = System.Configuration.ConfigurationManager.AppSettings["MinimumVoltage"];
            }

            MonitoringTable dt = Monitoring.FetchCriteria(startDate, endDate, null, null, Convert.ToDecimal(txtDifference.Text));

            //DataTable dt = Monitoring.FetchWaterLeveByDate("2009-05-10", "2009-05-15");

            if (dt == null || dt.Rows.Count == 0)
            {
                lblEmpty.Text = "<br />您所选定的时间段内，按最低电压" + txtDifference.Text + "来检查，无异常电压信息！";
                lblEmpty.Visible = true;
            }
            else
            {
                gvWarning.DataSource = dt;
                gvWarning.DataBind();

                lblEmpty.Visible = false;
            }
        }
    }
}
